package com.ildepinc.persistence.concretes.daos;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import com.ildepinc.persistence.abstracts.DaoTableAuditable;
import com.ildepinc.persistence.abstracts.TableAuditable;
import com.ildepinc.persistence.concretes.models.Language;
import com.ildepinc.persistence.concretes.models.LanguageSequence;
import com.ildepinc.persistence.interfaces.JdbcConnectionBehaviour;

public class DaoLanguage extends DaoTableAuditable {

  public DaoLanguage() {
    this.clear();
  }

  public DaoLanguage(JdbcConnectionBehaviour connection, String schema) {
    this.clear();
    this.setJdbcConnection(connection);
    this.setTable(new Language(schema));
    this.setSqlCommand(this.getTable().getSqlCreateTable());
    this.setSqlParameters(new ArrayList<Object>());
    this.setSequence(new LanguageSequence(schema));
  }

  @Override
  public TableAuditable copyRecordTo() {
    Language language = (Language) this.getTable();
    return language;
  }

  @Override
  public Integer doDelete(HashMap<String, Object> primaryKeys) throws Exception {
    Integer rowsAffected = null;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(primaryKeys.get("ID"));
    this.setSqlCommand(this.getTable().getSqlDelete());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(null);
    return rowsAffected;
  }

  @Override
  public Integer doInsert(TableAuditable record, HashMap<String, Object> primaryKeys) throws Exception {
    Integer rowsAffected = null;
    Language recordToInsert = (Language) record;
    this.getSqlParameters().clear();
    if( this.getTable().isPkSingleColumn() ) {
      Long newPk = this.getNewProgressive(
        this.getTable().getNewProgressiveMethod(),
        this.getTable().getSchema(),
        this.getTable().getTableName(),
        "ID"
      );
      this.getSqlParameters().add(newPk);
      recordToInsert.getPrimaryKeys().put("ID", newPk);
    }
    // if( (primaryKeys != null) && !primaryKeys.isEmpty() ) {
    //   this.getTable().setPrimaryKeys(primaryKeys);
    //   primaryKeys.forEach(
    //     (key, value)
    //       -> this.getSqlParameters().add(value)
    //   );
    // }
    this.getSqlParameters().add(recordToInsert.getIso6391());
    this.getSqlParameters().add(recordToInsert.getName());
    this.getSqlParameters().add(recordToInsert.getInsertedAt());
    this.getSqlParameters().add(recordToInsert.getInsertedBy());
    this.getSqlParameters().add(recordToInsert.getUpdatedAt());
    this.getSqlParameters().add(recordToInsert.getUpdatedBy());
    this.setSqlCommand(this.getTable().getSqlInsert());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(recordToInsert);
    return rowsAffected;
  }

  @Override
  public TableAuditable doSelect(HashMap<String, Object> primaryKeys) throws Exception {
    Language readRecord = null;
    ResultSet resultSet = null;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(primaryKeys.get("ID"));
    this.setSqlCommand(this.getTable().getSqlSelect());
    resultSet = this.doManualSelect();
    resultSet.first();
    this.readRecordFrom(resultSet);
    readRecord = (Language) this.copyRecordTo();
    this.setTable(readRecord);
    return readRecord;
  }

  @Override
  public Integer doUpdate(TableAuditable record) throws Exception {
    Integer rowsAffected = null;
    Language recordToUpdate = (Language) record;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(recordToUpdate.getIso6391());
    this.getSqlParameters().add(recordToUpdate.getName());
    this.getSqlParameters().add(recordToUpdate.getUpdatedAt());
    this.getSqlParameters().add(recordToUpdate.getUpdatedBy());
    this.getSqlParameters().add(recordToUpdate.getId());
    this.setSqlCommand(this.getTable().getSqlUpdate());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(recordToUpdate);
    return rowsAffected;
  }

  @Override
  public void readRecordFrom(ResultSet resultSet) throws Exception {
    Language language = (Language) this.getTable();
    language.setId(resultSet.getLong("ID"));
    language.getPrimaryKeys().put("ID", resultSet.getLong("ID"));
    language.setIso6391(resultSet.getString("ISO_639_1"));
    language.setName(resultSet.getString("NAME"));
    language.setInsertedAt(resultSet.getTimestamp("INSERTED_AT"));
    language.setInsertedBy(resultSet.getString("INSERTED_BY"));
    language.setUpdatedAt(resultSet.getTimestamp("UPDATED_AT"));
    language.setUpdatedBy(resultSet.getString("UPDATED_BY"));
    this.setTable(language);
  }

  @Override
  public void clear() {
    super.clear();
  }

}
